// pages/demo1/demo1.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list1: [
      { name: 'lihua', gender: '女' },
      { name: 'Jack', gender: '男' },
      { name: 'me', gender: '女' },
      { name: 'you', gender: '女' },
      { name: 'join', gender: '男' },
      { name: 'lisa', gender: '女' },
      { name: 'meimei', gender: '男' },
      { name: 'tina', gender: '女' },
      { name: 'ryi', gender: '男' },
      { name: 'lk', gender: '男' },
    ],
    list2: [
      { name: 'lihua2', gender: '女' },
      { name: 'Jack2', gender: '男' },
      { name: 'me2', gender: '女' },
      { name: 'you2', gender: '女' },
      { name: 'join2', gender: '男' },
      { name: 'lisa2', gender: '女' },
      { name: 'meimei2', gender: '男' },
      { name: 'tina2', gender: '女' },
      { name: 'ryi2', gender: '男' },
      { name: 'lk2', gender: '男' },
    ],
    pages: 1,
    value: ''
  },
  onLoad() {
    wx.setStorageSync('list1', this.data.list1)
    wx.setStorageSync('list2', this.data.list2)
  },
  handleInput(e) {
    const input = e.detail.value
    const query = input.trim()
    if(query){
    let copylist1 = this.data.list1
    const serchList = copylist1.filter(item => item.gender === query)
    this.setData({
      list1: serchList
    })
  }else {
    const list1 = wx.getStorageSync('list1')
    const list2 = wx.getStorageSync('list2')
  this.setData({
    list1,
    list2
  })
  }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重置筛选条件
    const list1 = wx.getStorageSync('list1')
    const list2 = wx.getStorageSync('list2')
    this.setData({
      list1,
      list2,
      value: '',
      pages: 1
    },function() {
      setTimeout(function() {
        wx.stopPullDownRefresh()
      },300)
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 当页面触底时，去加载第二页数组
    if(this.data.pages == 1) {
    wx.showLoading({
        title: '加载中',
        mask: true
    })
    this.setData({
      list1: [...this.data.list1, ...this.data.list2],
      pages: this.data.pages + 1
    },function() {
      setTimeout(function() {
        wx.hideLoading()
      },300)

    })
  }else {
    wx.showToast({
      title: '没有数据了',
      icon: 'none',
      duration: 1000,
      mask: false
    });
  }
  }

})